package com.xb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xb.model.domain.entity.Agreement;
import com.xb.system.mapper.db1.AgreementMapper;
import com.xb.system.service.AgreementService;
import org.springframework.stereotype.Service;

@Service
public class AgreementServiceImpl extends ServiceImpl<AgreementMapper, Agreement> implements AgreementService {

    @Override
    public String getAgreementContent(String type) {
        // 验证类型
        if (!"user".equals(type) && !"privacy".equals(type)) {
            throw new IllegalArgumentException("无效的协议类型: " + type);
        }

        // 查询最新的协议（按 update_time 降序）
        QueryWrapper<Agreement> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type)
                .orderByDesc("update_time")
                .last("LIMIT 1"); // 只取最新一条

        Agreement agreement = this.getOne(wrapper);
        if (agreement == null) {
            throw new RuntimeException("未找到对应类型的协议内容");
        }

        return agreement.getContent();
    }
}